﻿using CommunicationProtocol.dto;
using CommunicationProtocol.dto.command.client;
using CommunicationProtocol.dto.command.server;
using server.filters;
using server.gameLogic;
using server.threadContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunicationProtocol.dto.game;

namespace server.filter
{
    internal class RequestDecoderFilter : NetFilter
    {
        Random random = new Random();

        public void invoke(object buffer, InvocationChain invocationChain)
        {
            if (!(buffer is byte[])) {

                throw new ArgumentException("不支持"+buffer.GetType());
            }

            byte[] bytes = (byte[])buffer;  

            byte requestType = bytes[0];

            byte[] remainingBytes = new ArraySegment<byte>(bytes,1,bytes.Length-1).ToArray();

            ClientMetaData clientMetaData = ClientMetaDataContext.get();
            GameDataContext gameDataContext = GameDataContext.get();

            Command command = null;
            switch (requestType)
            {
                // client->server 的一对一调用请求
                // 服务器需要响应此请求
                // 类似 http
                case 1: {


                        // 请求可选的植物列表
                        List<StaticPlant> plants = gameDataContext.buyablePlants;

                        command = new RenderBuyablePlantListCommand(plants);

                        break;
                    }
                case 2: {
                        // 判断这个植物是否已经冷却完成
                        short objectId = (short)((bytes[1]<<8) + bytes[2]);

                        

                        int remainingMillisecond = gameDataContext.getPlantCoolingDownRemainingSeconds(objectId,clientMetaData.userId,out string currentOwnerId);

                        command = new PlantBuyableStatusResp(objectId,(short) remainingMillisecond, currentOwnerId);
                        
                        break ;
                    }
                case 3: {
                        // 从 可选的植物列表里面请求种植一棵植物
                        PlantAPlant plantAPlant = new PlantAPlant(remainingBytes);

                        DynamicPlant dynamicPlant = gameDataContext.plantANewPlant(plantAPlant, clientMetaData.userId);
                        
                        if (dynamicPlant ==null)
                        {
                            Console.WriteLine("种植植物失败"+plantAPlant);
                            return;
                        }
                        command = new CreatingPlantCommand(dynamicPlant);

                        AllClientHolder.send(command);
                        return;
                    }
                case 5:
                {
                    // 把服务器中的地图数据全部传输给用户

                    List<DynamicPlant> allPlantsOnMap = gameDataContext.allPlantsOnMap();
                    command = new CreatingPlantCommand(allPlantsOnMap);
                    break;
                }
                case 6:
                {
                    var otherPlayerPlantingCommand = new OtherPlayerPlantingCommand(remainingBytes,clientMetaData.userId);
                    AllClientHolder.sendExceptMe(otherPlayerPlantingCommand,clientMetaData.userId);
                    return;
                }
                default: {

                        throw new ArgumentException("不支持的请求方法 "+requestType);
                    }
            }

            clientMetaData.write(command);

        }
    }
}
